//
//  FXCSecondHome.m
//  XinLangFeiBo
//
//  Created by dllo on 16/3/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FXCSecondHome.h"
#import "FXCStatusCell.h"
#import "FXCcommentController.h"


@interface FXCSecondHome ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation FXCSecondHome

- (NSMutableArray *)arrModel{
    if (_arrModel == nil) {
        _arrModel = [NSMutableArray array];
    }
    return _arrModel;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.arrModel addObject:_statusF];
    
    [self creatTabelView];
    
    [self getInfo];
    
    [self setUpRefreshView];
    

    
    
}

//    添加下拉刷新控件
- (void)setUpRefreshView{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getInfo];
    }];
    
}




- (void)getInfo{
    NSString *url = @"https://open.weibo.cn/2/comments/show.json";    
    
    
    [FXCNetworking getWithURL:url parameter:@{@"id":[NSNumber numberWithLongLong:self.Id]}success:^(id responseObject) {
        
        for (NSDictionary *dic in responseObject[@"comments"]) {
            FXCStatus *comment = [FXCStatus mj_objectWithKeyValues:dic];
            [self.arrModel addObject:comment];
            [self.tableView reloadData];
        }

        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
        
    
}


- (void)creatTabelView{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell2";
    
    
    if (indexPath.row == 0) {
        static NSString *ID = @"cell";
        
        FXCStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[FXCStatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        
        FXCStatusFrame *statusF = self.arrModel[indexPath.row];
        cell.statusF = statusF;
        
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:ID];
        }
        
        FXCStatus *comments = self.arrModel[indexPath.row];
        cell.textLabel.text = comments.user.name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"评论:   %@", comments.text];
        
        [cell.imageView sd_setImageWithURL:comments.user.avatar_large placeholderImage:[UIImage imageNamed:@"60"]];
        
        
        return cell;
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        FXCStatusFrame *statusF = self.arrModel[indexPath.row];
        return statusF.cellHeight;
    } else {
        return 80;
    }
    
    
}

@end
